//
//  MyProfitViewController.m
//  UniversalApp
//
//  Created by 志刚杨 on 2018/3/20.
//  Copyright © 2018年 voidcat. All rights reserved.
//

#import "MyProfitViewController.h"
#import "RootWebViewController.h"
#import "MyProfitViewTableViewCell.h"
#import "IncomeModel.h"
#import "RechargeViewController.h"
@interface MyProfitViewController ()<QMUITableViewDelegate,QMUITableViewDataSource>

@property(nonatomic, strong) UIButton *backButton;//返回按钮
@property(nonatomic, strong) UILabel *labtitle;//标题
@property(nonatomic, strong) UIButton *detailedButton;//明细
@property(nonatomic, strong) UILabel *totalCoinLab;//总聊币
@property(nonatomic, strong) UILabel *totalCoinNumber;//总聊币数量


@property(nonatomic, strong) UILabel *totalDayCoinLab;//今日可获得收益
@property(nonatomic, strong) UILabel *totalDayCoinNumber;//今日可获得收益数量


@property(nonatomic, strong) UIImageView *headerImageView;//头部背景
@property(nonatomic, strong) QMUIFillButton *myProfit;//我的收益按钮
@property(nonatomic, strong) NSArray *incomeModelList;
@end

@implementation MyProfitViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}

-(void)setupUI
{
    self.isHidenNaviBar = YES;
    _backButton = [[UIButton alloc] init];
    //    _backButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
    [_backButton addTarget:self action:@selector(handleBackEvent:) forControlEvents:UIControlEventTouchUpInside];
    [_backButton setImage:IMAGE_NAMED(@"icon_back") forState:UIControlStateNormal];
    
    _detailedButton = [[UIButton alloc] init];
    [_detailedButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_detailedButton addTarget:self action:@selector(handleDetailEvent) forControlEvents:UIControlEventTouchUpInside];
    
    [_detailedButton setTitle:ASLocalizedString(@"明细")forState:UIControlStateNormal];
    _detailedButton.hidden = YES;
    _detailedButton.titleLabel.font = DEFAULT_FONT(13);
    //    _detailedButton.backgroundColor = [UIColor blackColor];
    _labtitle = [[UILabel alloc] init];
    _labtitle.text = ASLocalizedString(@"我的收益");
    _labtitle.textColor = [UIColor whiteColor];
    
    _totalCoinLab = [[UILabel alloc] init];
    _totalCoinLab.text  = ASLocalizedString(@"总收益");
    _totalCoinLab.font = DEFAULT_FONT(14);
    _totalCoinLab.textColor = KWhiteColor;
    _totalCoinLab.textAlignment = NSTextAlignmentCenter;
    _totalDayCoinLab = [[UILabel alloc] init];
    _totalDayCoinLab.text  = ASLocalizedString(@"今日可提现金额(元)");
    _totalDayCoinLab.font = DEFAULT_FONT(14);
    _totalDayCoinLab.textColor = KWhiteColor;
    _totalDayCoinLab.textAlignment = NSTextAlignmentCenter;
    
    
    
    _totalCoinNumber = [[UILabel alloc] init];
    _totalCoinNumber.text = @"...";
    _totalCoinNumber.textColor = [UIColor whiteColor];
    _totalCoinNumber.font =  [UIFont fontWithName:@"Helvetica-Bold" size:28];
    _totalCoinNumber.textAlignment = NSTextAlignmentCenter;
    
    _totalDayCoinNumber = [[UILabel alloc] init];
    _totalDayCoinNumber.text = @"...";
    _totalDayCoinNumber.textColor = [UIColor whiteColor];
    _totalDayCoinNumber.font =  [UIFont fontWithName:@"Helvetica-Bold" size:28];
    _totalDayCoinNumber.textAlignment = NSTextAlignmentCenter;
    
    
    _headerImageView = [[UIImageView alloc] init];
    _headerImageView.backgroundColor = MainColor;
    
    [self.view addSubview:_headerImageView];
    
    [_headerImageView addSubview:_labtitle];
    [_headerImageView addSubview:_totalCoinLab];
    [_headerImageView addSubview:_totalCoinNumber];
    
    //今日收益
    [_headerImageView addSubview:_totalDayCoinLab];
    [_headerImageView addSubview:_totalDayCoinNumber];
    
    _myProfit = [[QMUIFillButton alloc] initWithFillColor:MainColor titleTextColor:KWhiteColor];
    [_myProfit setTitle:ASLocalizedString(@"收益提现")forState:UIControlStateNormal];
    [_myProfit addTarget:self action:@selector(getChah) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:_detailedButton];
    [self.view addSubview:_backButton];
    [self.view addSubview:_myProfit];

    self.tableView.frame = CGRectFlatMake(0, 0, 0, 0);
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    
    //注册cell
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([MyProfitViewTableViewCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([MyProfitViewTableViewCell class])];
    
    self.tableView.mj_header.hidden = YES;
    self.tableView.mj_footer.hidden = YES;
    [self.tableView reloadData];
    [self loadData];
    [self setLayout];
}

-(void)getChah
{
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:ASLocalizedString(@"提示")message:ASLocalizedString(@"输入提款信息")preferredStyle:UIAlertControllerStyleAlert];
    
    //增加确定按钮；
    
    [alertController addAction:[UIAlertAction actionWithTitle:ASLocalizedString(@"确定")style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        UITextField * firstKeywordTF = [[UITextField alloc]init];
        
        UITextField * secondKeywordTF = [[UITextField alloc]init];
        
        
        NSArray * textFieldArr = @[firstKeywordTF,secondKeywordTF];
        
        textFieldArr = alertController.textFields;
        
        UITextField * tf1 = alertController.textFields[0];
        
        UITextField * tf2 = alertController.textFields[1];
        
        
        NSString *url = [[CYURLUtils sharedCYURLUtils] makeURLWithC:@"user_api" A:@"cash_income"];
        
        if(!StrValid(tf1.text))
        {
            [HUDHelper alert:ASLocalizedString(@"姓名不能为空")];
            alertController.textFields[0].text = tf1.text;
            [self presentViewController:alertController animated:true completion:nil];

            return;
        }
        
        if(!StrValid(tf2.text))
        {
            [HUDHelper alert:ASLocalizedString(@"收款账号不能为空")];
            alertController.textFields[1].text = tf2.text;
            [self presentViewController:alertController animated:true completion:nil];

            return;
        }
        
        NSMutableDictionary *param = [NSMutableDictionary dictionary];
        [param setObject:SafeStr(tf1.text) forKey:@"name"];
        [param setObject:SafeStr(tf2.text) forKey:@"number"];
        
        [param setObject:SafeStr(tf1.text) forKey:@"gathering_name"];
        [param setObject:SafeStr(tf2.text) forKey:@"gathering_number"];
        
        [[HUDHelper sharedInstance] syncLoading:ASLocalizedString(@"提现中..")];
        [CYNET POSTv2:url parameters:param responseCache:^(id responseObject) {
            
        } success:^(id responseObject) {
            [[HUDHelper sharedInstance] syncStopLoading];
            [[HUDHelper sharedInstance] tipMessage:ASLocalizedString(@"提现成功")delay:0.5];
            [self loadData];
        } failure:^(NSString *error) {
            [[HUDHelper sharedInstance] syncStopLoading];
            [MBProgressHUD showTopTipMessage:error];
        } hasCache:NO];
        
        
    }]];
    
    //增加取消按钮；
    
    [alertController addAction:[UIAlertAction actionWithTitle:ASLocalizedString(@"取消")style:UIAlertActionStyleDefault handler:nil]];
    
    //定义第一个输入框；
    
    [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        
        textField.placeholder = ASLocalizedString(@"姓名");
        
    }];
    
    //定义第二个输入框；
    
    [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        
        textField.placeholder = ASLocalizedString(@"支付宝账号");
        
    }];
    
    [self presentViewController:alertController animated:true completion:nil];
    
    

}

- (void)handleDetailEvent {

}

-(void)loadData
{
    NSString *url = [[CYURLUtils sharedCYURLUtils] makeURLWithC:@"page_data_api" A:@"get_user_income_page_info"];
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    //[param setObject:@"" forKey:@""];
    
    [CYNET POSTv2:url parameters:param responseCache:^(id responseObject) {
        
    } success:^(id responseObject) {
        
        _totalCoinNumber.text = [NSString stringWithFormat:@"%@",[responseObject valueForKey:@"income"]];
        _totalDayCoinNumber.text = [NSString stringWithFormat:@"%@",[responseObject valueForKey:@"money"]];
        _incomeModelList = [NSArray modelArrayWithClass:[IncomeModel class] json:[responseObject valueForKey:@"list"]];
        
        [self.tableView reloadData];
        
    } failure:^(NSString *error) {
        [MBProgressHUD showTopTipMessage:error];
    } hasCache:NO];
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _incomeModelList.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 65;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MyProfitViewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([MyProfitViewTableViewCell class]) forIndexPath:indexPath];
    IncomeModel *mdoel = _incomeModelList[indexPath.row];
    [cell setModel:mdoel];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

-(void)handleBackEvent:(UIButton *)button
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)setLayout
{
    [_headerImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self.view);
        make.height.equalTo(self.view).multipliedBy(0.35);
        make.top.equalTo(self.view);
        make.left.equalTo(self.view);
    }];
    
    [_backButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(10);
        make.top.equalTo(self.view).offset(kStatusBarHeight);
        make.width.equalTo(@40);
        make.height.equalTo(@40);
    }];
    
    [_detailedButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view.mas_right).offset(-20);
        make.width.equalTo(@40);
        make.centerY.equalTo(_backButton);
        make.height.equalTo(@40);
    }];
    
    [_labtitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.centerY.equalTo(_backButton);
    }];
    
    [_totalCoinLab mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerX.equalTo(self.view);
        make.top.equalTo(_labtitle.mas_bottom).offset(25);
        make.height.equalTo(@20);
    }];
    
    [_totalCoinNumber mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(_totalCoinLab);
        make.top.equalTo(_totalCoinLab.mas_bottom).offset(25);
    }];
    
    
    [_totalDayCoinLab mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerX.equalTo(self.view);
        make.top.equalTo(_labtitle.mas_bottom).offset(25);
        make.height.equalTo(@20);
    }];
    
    
    [_totalDayCoinNumber mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(_totalDayCoinLab);
        make.top.equalTo(_totalCoinLab.mas_bottom).offset(25);
    }];
    
    [@[_totalCoinLab,_totalDayCoinLab] mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedItemLength:140 leadSpacing:20 tailSpacing:20];
    
    
    [_myProfit mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.bottom.equalTo(self.view).offset(-80);
        make.width.equalTo(self.view).offset(-70);
        make.height.equalTo(@40);
    }];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_headerImageView.mas_bottom);
        make.left.and.right.equalTo(_headerImageView);
        make.width.equalTo(self.view);
        make.bottom.equalTo(_myProfit).offset(-88);
    }];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
